package com.dreamer.repository.lottery;

import com.dreamer.domain.lottery.Lottery;
import com.dreamer.domain.lottery.vo.UserVo;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.springframework.stereotype.Repository;
import ps.mx.otter.utils.SearchParameter;
import ps.mx.otter.utils.dao.hibernate.HibernateBaseDAO;

import java.util.ArrayList;
import java.util.List;

@Repository("lotteryDAO")
public class LotteryDAO extends HibernateBaseDAO<Lottery> {

    private Session getCurrentSession() {
        return getSessionFactory().getCurrentSession();
    }

    public List<Lottery> searchAllByPage(SearchParameter<Lottery> p) {
        return super.searchEntityByPage(p, (t) -> {
            Example example = Example.create(t.getEntity()).enableLike(MatchMode.ANYWHERE);
            Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getClazz());
            criteria.add(example);
            return criteria;
        }, (o)-> true);
    }

    public List<UserVo> findLotteryVoAll() {
        List<UserVo> userVoList = new ArrayList<>();
        String hql = " select l.lotteryId as lotteryId,u.id as userId,u.realName as realName," +
                " u.mobile as phone,a.name as activityName,p.cjCode as cjCode, a.code as code," +
                " p.prizeId as prizeId,p.name as name,p.names as names,p.redeemCode as redeemCode, " +
                " p.level as level,l.time as time,l.codes as codes " +
                " from Lottery as l left join " +
                " User as u on u.id = l.userId" +
                " LEFT JOIN Activity as a on a.code = l.activityCode" +
                " LEFT JOIN Prize as p on p.prizeId = l.prizeId ";
        Query query = getCurrentSession().createQuery(hql);
        List list = query.list();
        for (int i = 0; i < list.size(); i++) {
            Object[] object = (Object[]) list.get(i);
            UserVo userVo = new UserVo();
            userVo.setLotteryId((Integer) object[0]);
            userVo.setUserId((Integer) object[1]);
            userVo.setRealName((String) object[2]);
            userVo.setPhone((String) object[3]);
            userVo.setActivityName((String) object[4]);
            userVo.setCjCode((String) object[5]);
            userVo.setCode((String) object[6]);
            userVo.setPrizeId((Integer) object[7]);
            userVo.setName((String) object[8]);
            userVo.setNames((String) object[9]);
            userVo.setRedeemCode((String) object[10]);
            userVo.setLevel((String) object[11]);
            userVo.setTime((String) object[12]);
            userVo.setCodes((String) object[13]);
            userVoList.add(userVo);
        }
        return userVoList;
    }


    public void save(Lottery transientInstance) {
        try {
            getCurrentSession().save(transientInstance);
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public void delete(Lottery persistentInstance) {
        try {
            getCurrentSession().delete(persistentInstance);
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public Lottery findById(Integer id) {
        try {
            Lottery instance = (Lottery) getCurrentSession().get("com.dreamer.domain.lottery.Lottery", id);
            return instance;
        } catch (RuntimeException re) {

            throw re;
        }
    }

    public Lottery merge(Lottery detachedInstance) {
        try {
            Lottery result = (Lottery) getCurrentSession().merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public List<Lottery> findLotteryByUserId(Integer userId){
        String queryString = "from Lottery as l where l.userId="+ userId + " ORDER BY l.time DESC ";
        Query query = getCurrentSession().createQuery(queryString);
        return query.list();
    }

}
